package com.newtouch.common;


/**
 * 分页对象. 用于包含分页信息的对象，Page类实现了用于显示分页信息的基本方法
 * 
 * @author mengxk
 */
public class Page implements java.io.Serializable {

	private static final long serialVersionUID = 7149086987605232132L;
	public static final int DEFAULT_PAGE_SIZE = 10;

	/**
	 * 每页的记录数，实际记录数小于或等于它
	 */
	private int pageSize = DEFAULT_PAGE_SIZE;
	/**
	 * 目标页号
	 */
	private int targetPage = 1;
	/**
	 * 总页数
	 */
	private int totalPages = 1;
	/**
	 * 当前查询第一条数据在数据库中的位置
	 */
	private int startRow = 0;
	/**
	 * 当前查询最后一条数据在数据库中的位置
	 */
	private int endRow = DEFAULT_PAGE_SIZE;
	/**
	 * 总记录数
	 */
	private int totalRows = 0;
	
	private MapBean map = null;

	/**
	 * 构造方法，只构造空页
	 */
	public Page() {		
	}
	/**
	 * 计算分页信息：总页数、开始行数、结束行数
	 * 需要数据：总记录数、跳转页、页记录数
	 */
	public void computer(){		
		this.totalPages = (totalRows + pageSize - 1) / pageSize;
		if (totalRows <= 0 || totalPages <= 0) {
			this.totalPages = 1;
		}	
		//this.targetPage = (startRow - 1) / pageSize + 1;
		if (targetPage > totalPages) {
			this.targetPage = totalPages;
		} else if (targetPage < 1) {
			this.targetPage = 1;
		}
		// 查询使用标记
		this.startRow = (targetPage - 1) * pageSize;// + 1;
		this.endRow = startRow + pageSize ;
	}
	/**
	 * 根据总行数进行分页计算
	 * @param totalRows
	 */
	public void computer(int totalRows){
		this.totalRows = totalRows;
		this.computer();
	}
	/**
	 * 根据总行数、跳转页、页记录数 进行分页计算
	 * @param totalRows
	 * @param targetPage
	 * @param pageSize
	 */
	public void computer(int totalRows, int targetPage, int pageSize){
		this.totalRows = totalRows;
		this.targetPage = targetPage;
		this.pageSize = pageSize;
		this.computer();
	}
	/**
	 * 根据起始行和结束行进行分页计算
	 * @param startRow
	 * @param endRow
	 */
	public void computer(int startRow, int endRow){
		this.startRow = startRow;
		this.endRow = endRow;
		this.pageSize = endRow - startRow + 1;		
	}
	/**
	 * 是否有下一页
	 */
	public boolean hasNextPage() {
		return (this.getTargetPage() < this.getTotalPages());
	}
	/**
	 * 是否有上一页
	 */
	public boolean hasPreviousPage() {
		return (this.getTargetPage() > 1);
	}
	
	/**
	 * @return Returns the pageSize.
	 */
	public int getPageSize() {
		return pageSize;
	}
	/**
	 * @param pageSize The pageSize to set.
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	/**
	 * @return Returns the targetPage.
	 */
	public int getTargetPage() {
		return targetPage;
	}
	/**
	 * @param targetPage The targetPage to set.
	 */
	public void setTargetPage(int targetPage) {
		this.targetPage = targetPage;
	}
	/**
	 * @return Returns the totalPages.
	 */
	public int getTotalPages() {
		return totalPages;
	}
	/**
	 * @param totalPages The totalPages to set.
	 */
	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}
	/**
	 * @return Returns the startRow.
	 */
	public int getStartRow() {
		return startRow;
	}
	/**
	 * @param startRow The startRow to set.
	 */
	public void setStartRow(int startRow) {
		this.startRow = startRow;
	}
	/**
	 * @return Returns the endRow.
	 */
	public int getEndRow() {
		return endRow;
	}
	/**
	 * @param endRow The endRow to set.
	 */
	public void setEndRow(int endRow) {
		this.endRow = endRow;
	}
	/**
	 * @return Returns the totalRows.
	 */
	public int getTotalRows() {
		return totalRows;
	}
	/**
	 * @param totalRows The totalRows to set.
	 */
	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}
	/**
	 * @return Returns the map.
	 */
	public MapBean getMap() {
		if(map == null){
			map = new MapBean();
		}
		map.put("startRow",getStartRow());
		map.put("endRow",getEndRow());
		MapUtil.strutsActionMap(map);
		return map;
	}
	/**
	 * @param map The map to set.
	 */
	public void setMap(MapBean map) {
		this.map = map;
	}	
	
	public String toString(){
		return "class_name:"+getClass().getName()+";pageSize:"+getPageSize()+";targetPage:"+getTargetPage()+";totalPages:"+getTotalPages()+";totalRows:"+getTotalRows()+";startRow:"+getStartRow()+";endRows:"+getEndRow();
	}
	
	public static void main(String[] args){
		Page p = new Page();
		System.out.println(p);
		p.setPageSize(5);
		p.computer(16);
		p.setTargetPage(1);
		System.out.println(p);
	}

}
